// mock数据入口文件

// 导入所有的mock模块
import { mockLogin, mockGetUserInfo, mockLogout } from './auth';
import axios from 'axios';

// 导出所有的mock函数
export {
  mockLogin,
  mockGetUserInfo,
  mockLogout,
};

// 初始化mock数据
export const initMockData = () => {
  console.log('初始化Mock数据');
  setupMockData();
};

// 设置mock数据
const setupMockData = () => {
  // 使用axios拦截器模拟API响应
  axios.interceptors.request.use(async (config) => {
    // 模拟登录接口 - 考虑到request.jsx中配置的baseURL
    if ((config.url === '/auth-center/auth/login' || config.url === 'http://localhost:8080/api/auth-center/auth/login') && config.method === 'post') {
      const { username, password } = JSON.parse(config.data);
      return mockLogin(username, password)
        .then(data => {
          return Promise.resolve({ data });
        })
        .catch(error => {
          return Promise.reject(error);
        });
    }
    
    // 模拟获取用户信息接口
    if ((config.url === '/auth-center/auth/getCurrentUserInfo' || config.url === 'http://localhost:8080/api/auth-center/auth/getCurrentUserInfo') && config.method === 'get') {
      return mockGetUserInfo()
        .then(data => {
          return Promise.resolve({ data });
        })
        .catch(error => {
          return Promise.reject(error);
        });
    }
    
    // 模拟登出接口
    if ((config.url === '/auth-center/auth/logout' || config.url === 'http://localhost:8080/api/auth-center/auth/logout') && config.method === 'post') {
      return mockLogout()
        .then(data => {
          return Promise.resolve({ data });
        })
        .catch(error => {
          return Promise.reject(error);
        });
    }
    
    return config;
  });
};

// 如果是开发环境，自动初始化mock数据
if (process.env.NODE_ENV === 'development') {
  initMockData();
}